#ifndef SYNCEDMEM_COMMON_H
#define SYNCEDMEM_COMMON_H

#include <cstdlib>
#include<iostream>

#define checkCudaErrors( a ) do { \
    if (cudaSuccess != (a)) { \
    fprintf(stderr, "Cuda runtime error in line %d of file %s \
    : %s \n", __LINE__, __FILE__, cudaGetErrorString(cudaGetLastError()) ); \
    exit(EXIT_FAILURE); \
    } \
    } while(0);

namespace algocommon
{

class SyncedMemory //最底层，显存内存以及指针index位置转换
{
public:
	SyncedMemory();
	explicit SyncedMemory(size_t size);
	~SyncedMemory();
	const void *cpu_data();
	const void *gpu_data();
	void *mutable_cpu_data();
	void *mutable_gpu_data();
	void memset_gpu_data();
	enum SyncedHead
	{
		UNINITIALIZED,
		HEAD_AT_CPU,
		HEAD_AT_GPU,
		SYNCED
	};

private:
	void to_cpu();
	void to_gpu();
	void *cpu_ptr_;
	void *gpu_ptr_;
	size_t size_;
	SyncedHead head_;
	bool own_cpu_data_;
	bool own_gpu_data_;
};
} // namespace algocommon

#endif